package clark.model

/**
 * Created by IntelliJ IDEA.
 * @author Cui Gang
 * email:<a href="javascript:mailto">clarkhillm@gmail.com</a>
 */

import com.trolltech.qt.gui._
import clark.resource._
import com.trolltech.qt.core.Qt
import java.sql.ResultSet
import java.text.SimpleDateFormat

object QTreeItemBuilder {
    val connection = Tools.connection

    def getRootItems: QTreeWidgetItem = {
        val root = new QTreeWidgetItem()
        root.setText(0, "日期")
        root.setIcon(0, Resource.close)
        root.setData(0, Qt.ItemDataRole.UserRole, None)
        deleteChildren(root)

        for (d <- getYears) {
            val child = new QTreeWidgetItem()
            child.setText(0, d)
            child.setIcon(0, Resource.close)
            child.setData(0, Qt.ItemDataRole.UserRole, "year")
            root.addChild(child)
        }
        root
    }

    def getRs(sql: String): ResultSet = {
        Vanguard.buildTable
        connection.createStatement.executeQuery(sql)
    }

    def getYears: List[String] = {
        val sql = "select distinct year(date) y from note order by y"
        val rs = getRs(sql)
        var t = List[String]()
        Tools.Rs(rs) {
            t = t.+:(rs.getString("y"))
        }
        t
    }

    def getMonth(year: QTreeWidgetItem) {
        deleteChildren(year)
        val y = year.text(0)
        val sql = "select distinct month(date) month from note where year(date)=" + y + " order by month desc"
        //println(sql)
        val rs = getRs(sql)
        Tools.Rs(rs) {
            val m = new QTreeWidgetItem
            m.setText(0, rs.getString("month"))
            m.setIcon(0, Resource.close)
            m.setData(0, Qt.ItemDataRole.UserRole, "month")
            year.addChild(m)
        }
    }

    def deleteChildren(item: QTreeWidgetItem) {
        for (c <- item.takeChildren.toArray(Array[QTreeWidgetItem]()))
            item.removeChild(c)
    }

    def getDay(month: QTreeWidgetItem) {
        deleteChildren(month)
        val y = month.parent.text(0)
        val m = month.text(0)
        val sql = "select distinct day(date) day from note where year(date)=" + y + " and month(date)=" + m + " order by day desc"
        val rs = getRs(sql)
        Tools.Rs(rs) {
            val d = new QTreeWidgetItem
            d.setText(0, rs.getString("day"))
            d.setIcon(0, Resource.close)
            d.setData(0, Qt.ItemDataRole.UserRole, "day")
            month.addChild(d)
        }
    }

    def getNote(day: QTreeWidgetItem) {
        deleteChildren(day)
        val y = day.parent.parent.text(0)
        val m = day.parent.text(0)
        val d = day.text(0)
        val sql = "select * from note where year(date)=" + y + " and month(date)=" + m + " and day(date)=" + d + " order by date desc"
        val rs = getRs(sql)
        for (note <- NoteTool.getNotes(rs)) {
            val n = new QTreeWidgetItem
            n.setIcon(0, Resource.file)
            n.setText(0, note.title + "(" + new SimpleDateFormat("HH:mm:ss").format(note.date) + ")")
            n.setData(0, Qt.ItemDataRole.UserRole, note)
            day.addChild(n)
        }
    }
}